/****************************************************************************
 * 2025.5 小管
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;
using Unity.VisualScripting;
using QAssetBundle;

namespace ProjectSurviver
{
	public partial class AchievementController : UIElement
	{
		ResLoader mResLoader = ResLoader.Allocate();

		private void Awake()
		{
			var originLocalPosY = AchievementItem.LocalPositionY();
			var iconAtlas = mResLoader.LoadSync<SpriteAtlas>("icon");
			AchievementSystem.OnAchievementUnlocked.Register(item =>
			{
				Title.text = $"<b>成就 {item.Name} 达成!</b>";
				Description.text = item.Description;
				var sprite = iconAtlas.GetSprite(item.IconName);
				Icon.sprite = sprite;
				AchievementItem.Show();
				AchievementItem.LocalPositionY(-200);
				AudioKit.PlaySound(Sfx.ACHIEVEMENT);

				ActionKit.Sequence() // 序列动作
				.Lerp(-200, originLocalPosY, 0.3f, (y) => 
				{
					AchievementItem.LocalPositionY(y); 
				}) 
				.Delay(2) // 显示 2 秒
				.Lerp(originLocalPosY, -200, 0.3f, (y) =>  
				{
					AchievementItem.LocalPositionY(y); // 注意：这里需要传递 y 作为参数
				}, () =>
				{
					AchievementItem.Hide(); // 隐藏成就项
				})
				.Start(this); // 注意：这里需要传递 this 作为上下文，确保在正确的生命周期内执行
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}

		protected override void OnBeforeDestroy()
		{
			mResLoader.Recycle2Cache();
			mResLoader = null;
		}
	}
}